//
//  AdressSelectController.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/30.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class AdressSelectController: BaseViewController {
    

    var selectID : String?
    
    weak var shopCarController : ShopCarOrderVC?
    
    weak var buyNowController : BuyNowOrderVC?

    var dataModels : [AddressInfoModel]?


    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "收货地址"
        
        plainTableView.rowHeight = UITableViewAutomaticDimension
        
        plainTableView.estimatedRowHeight = 100
        
        plainTableView.tableFooterView = UIView()
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "新建", style: .plain, target: self, action: #selector(pushAdressAdd))
        
        
        
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: AdressSelectCell.self), bundle: nil), forCellReuseIdentifier: AdressSelectCell.identifer())
        
        
        weak var weakSelf = self
        plainTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadAdressListData()
            
        }
        
        plainTableView.mj_header.beginRefreshing()
        


    }
    
    
    func pushAdressAdd() {
        
        let temp = AddAdressController()
        
        temp.block = {() ->() in
            
            
            self.loadAdressListData()
            
        }
        
        

        
        navigationController?.pushViewController(temp, animated: true)
        
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
    


    
}





extension AdressSelectController{
    
    
    
    func loadAdressListData() {
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_address&a=address_list", parameters: nil) { [weak self] (responseObject, error) in
            
            if error == nil{
                
                self?.dataModels = [AddressInfoModel]()
                
                for dic in responseObject!["datas"]!["address_list"] as! [[String : AnyObject]]{
                    
                    self?.dataModels?.append(AddressInfoModel(dic: dic))
                    
                }
                
                if self!.dataModels!.count == 0{
                    
                    self?.showLogoView(text: "您还没有收货地址哦!")
                    
                    self?.dataModels = nil
                    
                }else{
                    
                    self?.dataSort()
                    
                }
                
                
                
                self!.plainTableView.mj_header.endRefreshing()
                
                self!.plainTableView.reloadData()
                
                
            }else{
                
                
                self!.plainTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
                
                
            }
            
        }
        
        
        
    }
    
    
    
    func dataSort() {
        
        var temp : AddressInfoModel? = nil
        
        var idex = 0
        
        for (index,item) in dataModels!.enumerated() {
            
            if item.is_default == "1" {
                temp = item
                idex = index
            }
        }
        
        
        
        if temp != nil {
            dataModels?.remove(at: idex)
            dataModels?.insert(temp!, at: 0)
        }
        
        
    }
    
    
    
}






extension AdressSelectController{
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        return 1
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        
        return dataModels!.count
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let tableCell : AdressSelectCell =  tableView.dequeueReusableCell(withIdentifier: AdressSelectCell.identifer()) as! AdressSelectCell
        
        if selectID != nil {
            
            tableCell.didSelectID = selectID
        }
        
        tableCell.model = dataModels![indexPath.row]
        
        tableCell.delegateVC = self
        
        return tableCell
        
    }
    
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let model = dataModels![indexPath.row]
        
        if shopCarController != nil {
            
            shopCarController?.adressID = model.address_id
            
            shopCarController?.setDefulAdress()
            
            shopCarController?.loadData()
            
            navigationController?.popViewController(animated: true)
            
        }
        
        if buyNowController != nil {
            
            buyNowController?.adressID = model.address_id
            
            buyNowController?.setDefulAdress()
            
            buyNowController?.loadData()
            
            navigationController?.popViewController(animated: true)
            
            
        }
        
    }
    
    
}



